import '../utils/date_utils.dart';
import 'paging_model.dart';
import 'result_model.dart';

/// 学年学期-查询-请求
class SemesterQueryReq extends PagingQueryReq {
  final String? title;
  final int? status;
  final DateTime? now;

  SemesterQueryReq({
    this.title,
    this.status,
    this.now,
    super.index,
    super.size,
  });

  @override
  Map<String, dynamic> toMap() {
    final map = super.toMap();
    map['now'] = dateUtils.format('yyyy-MM-dd', now);
    map['title'] = title;
    map['status'] = status;
    return map;
  }
}

///学年学期-查询-响应
class SemesterQueryRes extends PagingQueryRes<SemesterItemRes> {
  SemesterQueryRes({
    required super.retCode,
    required super.retMessage,
    super.total,
    super.rows,
  });

  factory SemesterQueryRes.fromModel(final ResultModelRes res) {
    final ret = PagingQueryRes.fromModel(
      res,
      (row) => SemesterItemRes.fromMap(row),
    );
    return SemesterQueryRes(
      retCode: ret.retCode,
      retMessage: ret.retMessage,
      total: ret.total,
      rows: ret.rows,
    );
  }
}

/// 学年学期-数据项-响应
class SemesterItemRes {
  final String tenantId, id, code, title, remark;
  final DateTime? start, end, createdAt;
  final int isDefault, status;

  const SemesterItemRes({
    required this.tenantId,
    required this.id,
    required this.code,
    required this.title,
    required this.remark,
    required this.isDefault,
    required this.status,
    this.start,
    this.end,
    this.createdAt,
  });

  factory SemesterItemRes.fromMap(final Map<String, dynamic> map) =>
      SemesterItemRes(
        tenantId: map['tenantId'] ?? '',
        id: map['id'] ?? '',
        code: map['code'] ?? '',
        title: map['title'] ?? '',
        remark: map['remark'] ?? '',
        start: dateUtils.parse('yyyy-MM-dd', map['start']),
        end: dateUtils.parse('yyyy-MM-dd', map['end']),
        isDefault: map['isDefault'] ?? 0,
        status: map['status'] ?? 0,
        createdAt: dateUtils.parse('yyyy-MM-dd HH:mm:ss', map['createdAt']),
      );
}

class SemesterInfoRes extends SemesterItemRes with ModelRes {
  @override
  final int retCode;
  @override
  final String retMessage;

  SemesterInfoRes({
    required this.retCode,
    required this.retMessage,
    required SemesterItemRes item,
  }) : super(
          tenantId: item.tenantId,
          id: item.id,
          code: item.code,
          title: item.title,
          remark: item.remark,
          start: item.start,
          end: item.end,
          isDefault: item.isDefault,
          status: item.status,
          createdAt: item.createdAt,
        );

  factory SemesterInfoRes.fromModel(final ResultModelRes res) =>
      SemesterInfoRes(
        retCode: res.retCode,
        retMessage: res.retMessage,
        item: SemesterItemRes.fromMap(res.toMap),
      );
}

abstract class BaseSemesterSaveReq {
  final String code, title, remark;
  final int isDefault;
  final DateTime? start, end;

  const BaseSemesterSaveReq({
    required this.code,
    required this.title,
    required this.remark,
    required this.isDefault,
    this.start,
    this.end,
  });

  Map<String, dynamic> toMap() => {
        'code': code,
        'title': title,
        'remark': remark,
        'isDefault': isDefault,
        'start': dateUtils.format('yyyy-MM-dd', start),
        'end': dateUtils.format('yyyy-MM-dd', end),
      };
}

/// 学年学期-新增-请求
class SemesterAddReq extends BaseSemesterSaveReq {
  const SemesterAddReq({
    required super.code,
    required super.title,
    required super.remark,
    required super.isDefault,
    super.start,
    super.end,
  });
}

///学年学期-新增-请求
class SemesterModifyReq extends BaseSemesterSaveReq {
  final String id;
  final int? status;

  const SemesterModifyReq({
    required this.id,
    required super.code,
    required super.title,
    required super.remark,
    required super.isDefault,
    super.start,
    super.end,
    this.status,
  });

  @override
  Map<String, dynamic> toMap() {
    final map = super.toMap();
    map['status'] = status;
    return map;
  }
}
